package com.demo.dao;

import com.demo.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements UserDao {
    
    //依赖注入
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory=sqlSessionFactory;
    }
    @Override
    public User findUserById(int id) throws Exception{
        
        SqlSession sqlSession=sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("test.findUserById", id);
        sqlSession.close();
        return user;
    }
    
    @Override
    public List<User> findUserByName(String name) throws Exception{
        
        SqlSession sqlSession=sqlSessionFactory.openSession();
        List<User> list = sqlSession.selectList("test.findUserByName", name);
        sqlSession.close();
        return null;
    }
    
    @Override
    public void insertUser(User user) throws Exception{
        
        SqlSession sqlSession=sqlSessionFactory.openSession();
        user.setUsername("李四");
        user.setAddress("china");
        user.setSex("1");
    
        sqlSession.insert("test.insertUser",user);
        sqlSession.commit();
        sqlSession.close();
        
    }
    
    
}
